home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / GAMES / P_ROBO31 / HUGGER.PR < prev    next >
Text File  |  1993-02-15  |  7KB  |  199 lines

  1. (**************************************************************************)
  2. (*                             W A R N I N G                              *)
  3. (*                                                                        *)
  4. (*  This Robot does not work correctly. Fixing it is left as an exercise. *)
  5. (**************************************************************************)
  6.  
  7.   PROCEDURE Hugger;
  8.   {
  9.    Author: David Malmberg
  10.  
  11.    Strategy:  Start off by going to the middle of the battlefield
  12.               (if possible).  Then Scan for Obstructions and
  13.               move to an Obstruction and "hide" beside it by "hugging"
  14.               its walls.  While "protected" by the Obstruction, blast
  15.               away at ememies. If no enemies can be found, move to
  16.               another Obstruction (it there is one) and blast away
  17.               from it.  RaiseShield when moving still and lower them
  18.               when standing still and being "protected" by the
  19.               Obstruction.
  20.  
  21.               This Robot has been designed to utilize Fuel (if it
  22.               is available) to power its Shield.  It has also been
  23.               designed to take advantage of Obstructions.
  24.   }
  25.  
  26.   VAR { Hugger "Global" variables }
  27.  
  28.     Angle, { Scanning angle }
  29.     StartAngle, { Begining angle for "sizing up" Obstruction }
  30.     LowAngle, { Lowest angle for "Arc" of Obstruction }
  31.     HighAngle, { Highest angle for "Arc" of Obstruction }
  32.     Start, { Starting angle for scanning for enemies }
  33.     Finish, { Last angle for scanning for enemies }
  34.     Range, { Range/Distance to foe }
  35.     Sweep, { "Sweep count" -- when = 18, Robot has scanned 360 degrees }
  36.     Delta          : Integer; { Scanning arc }
  37.  
  38.  
  39.     PROCEDURE GOTO(X, Y : Integer);
  40.       { Go to location X,Y on playing field. }
  41.     VAR Heading    : Integer;
  42.     BEGIN
  43.     { WARNING:  If the point X,Y is inside an Obstruction then }
  44.     { executing this routine will cause the Robot to commit }
  45.     { suicide by repeatedly running into the wall of the Obstruction. }
  46.  
  47.       { Find the heading we need to get to the desired spot. }
  48.       Heading := Angle_To(x, y);
  49.  
  50.       { Keep traveling at top speed until we are within 150 meters }
  51.       WHILE (distance(loc_x, loc_y, x, y) > 150) DO drive(Heading, 100);
  52.  
  53.       { Cut speed, and creep the rest of the way. }
  54.       WHILE (distance(loc_x, loc_y, x, y) > 20) DO drive(Heading, 20);
  55.  
  56.       { Stop driving, should coast to a stop. }
  57.       drive(Heading, 0); {I.E., Stop}
  58.     END; {GoTo(X,Y)}
  59.  
  60.  
  61.     PROCEDURE Ramble(X, Y : Integer);
  62.       { Move to X, Y (if possible) on the playing field }
  63.       { by avoiding Obstructions - if any.              }
  64.     VAR Heading, Tries, Dist : Integer;
  65.     BEGIN
  66.       Tries := 0;
  67.       Heading := Angle_To(X, Y);
  68.       Drive(Heading, 50); {Start off toward X,Y}
  69.       Dist := Scan(Heading, 5);
  70.       REPEAT
  71.         IF ObjectScanned = Obstruction
  72.           THEN BEGIN
  73.             REPEAT
  74.               Heading := Heading + 10;
  75.               Dist := Scan(Heading, 5);
  76.             UNTIL ObjectScanned <> Obstruction;
  77.             Drive(Heading, 50); {Minimum speed to turn freely}
  78.           END;
  79.         Heading := Angle_To(X, Y);
  80.         Dist := Scan(Heading, 5);
  81.         Tries := Tries + 1;
  82.       UNTIL (ObjectScanned <> Obstruction) OR (Tries > 20);
  83.       IF (ObjectScanned <> Obstruction) THEN GOTO(X,Y);
  84.     END; {Ramble}
  85.  
  86.  
  87.     PROCEDURE Aim(VAR Ang : Integer; VAR Arc : Integer);
  88. {
  89.  Improve aim by doing a binary search of the target area.
  90.  I.E., divide the target area in two equal pieces and redefine
  91.  the target area to be the piece where the foe is found.
  92.  If the foe is not found, expand the search area to the
  93.  maximum arc of plus or minus 10 degrees.
  94. }
  95.     BEGIN
  96.       IF ObjectScanned = Enemy
  97.         THEN BEGIN
  98.           Arc := Arc DIV 2; { Divide search area in two. }
  99.           IF scan(Ang-Arc, Arc) <> 0 { Check piece "below" target angle. }
  100.             THEN Ang := Ang-Arc { If foe found, redefine target angle. }
  101.             ELSE IF scan(Ang+Arc, Arc) <> 0 { Check piece "above" target angle. }
  102.              THEN Ang := Ang+Arc { If foe found, redefine target angle. }
  103.              ELSE Arc := 10;
  104.           { Foe not found in either piece, expand search area to maximum arc. }
  105.         END
  106.         ELSE Arc := 10;
  107.     END; {Aim}
  108.  
  109.  
  110. PROCEDURE FindArc;
  111. { Find the "Arc" of the Obstruction -- if any }
  112. VAR Dist : Integer;
  113. BEGIN
  114.   Sweep := 0;
  115.   REPEAT
  116.     Sweep := Sweep + 1;
  117.     Start := Start + 20;
  118.     Dist := Scan(Start, 10)
  119.   UNTIL (ObjectScanned = Obstruction) OR (Sweep > 18);
  120.   IF Sweep <= 18 THEN
  121.     BEGIN
  122.       {Now find "highest" angle of "Arc" of the Obstruction}
  123.       HighAngle := Start - 5;
  124.       REPEAT
  125.         HighAngle := HighAngle + 5;
  126.         Dist := Scan(HighAngle,0);
  127.       UNTIL ObjectScanned <> Obstruction;
  128.       {Now find "Lowest" angle of "Arc" of the Obstruction}
  129.       LowAngle := Start + 5;
  130.       REPEAT
  131.         LowAngle := LowAngle - 5;
  132.         Dist := Scan(LowAngle,0);
  133.       UNTIL ObjectScanned <> Obstruction; {lowest angle might be negative}
  134.     END
  135.     ELSE {Sweep > 18 -- No Obstruction found}
  136.     BEGIN
  137.       LowAngle := 0; {Set to zero if no Obstruction found}
  138.       HighAngle := 0;
  139.     END;
  140. END; { FindArc }
  141.  
  142.  
  143. PROCEDURE GoToObstruction;
  144.  { Go to the Obstruction and "hug" its walls }
  145.  VAR Heading, Dist : Integer;
  146.   BEGIN {GoToObstruction}
  147.     RaiseShield; {Need protection when moving in open territory}
  148.     Heading := (HighAngle + LowAngle) DIV 2; {"middle" of Obstruction}
  149.     REPEAT
  150.       Drive(Heading, 100);
  151.       Dist := Scan(Heading,0);
  152.     UNTIL (Dist < 2) AND (ObjectScanned = Obstruction); {"Hug" the wall}
  153.     StartAngle := Heading;
  154.     FindArc; {New LowAngle and HighAngle calculated}
  155.     Start := HighAngle - 360; {"Arc" for scanning for enemies}
  156.     Finish := LowAngle;
  157.     LowerShield; {Obstruction will "protect" robot}
  158.   END; {GoToObstruction}
  159.  
  160.  
  161. PROCEDURE Blast_Em;
  162.  {Scan for enemies and blast 'em}
  163.   BEGIN {Blast_Em}
  164.     Sweep := 0;
  165.     Angle := Start + 10;
  166.     REPEAT
  167.       Delta := 10; { Start with widest scanning arc. }
  168.       Range := scan(Angle, Delta);
  169.       WHILE (Range > 40) AND (Range < 700) AND (ObjectScanned = Enemy)
  170.         DO { Must be far enough away to avoid self-damage. }
  171.         BEGIN
  172.           Sweep := 0; {Reset Sweep counter}
  173.           Aim(Angle, Delta); { Improve aim. }
  174.           IF ObjectScanned = Enemy
  175.             THEN cannon(Angle, Range); { Fire!! }
  176.           Range := scan(Angle, Delta); { Is foe still in sights? }
  177.         END;
  178.       Angle := Angle+20; { Look in adjacent target area. }
  179.       IF Angle >= Finish THEN
  180.         BEGIN
  181.           Angle := Start + 10;
  182.           Sweep := Sweep + 1;
  183.         END;
  184.     UNTIL Sweep > 10; {10 unsuccessful scans of whole area}
  185.   END; {Blast_Em}
  186.  
  187.  
  188.   BEGIN {Hugger Main}
  189.     RaiseShield;
  190.     Angle := 0;
  191.     Ramble(500, 500); { Move to center of field -- if possible. }
  192.     REPEAT { Until Dead or Winner }
  193.       FindArc; {Find Obstruction}
  194.       GoToObstruction; {"Hide" beside Obstruction}
  195.       Blast_Em; {Scan and Blast enemies}
  196.     UNTIL Dead OR Winner;
  197.   END; {Hugger Main}
  198.  
  199.